#!/usr/bin/python2.5

#
# developer tool: script for update codebase on a production server
#

import os, sys

makeabs = lambda f: (f.startswith('.') and os.path.join(os.getcwd(),f)) or f
def createConf(container,conf):
  """
  """
  f=open(conf,'w')
  projpath = makeabs(container)
  prodhost = raw_input('enter production-host name > ').strip()
  produser = raw_input('enter production-user name > ').strip()
  C = locals()
  del C['f']
  del C['container']
  del C['conf']
  f.write(str(C))
  f.close()
  return eval(open(conf).read())

def getOrCreateConf(container):
    """
    """
    conf      = os.path.join(container,'.dsand_conf')
    if not os.path.exists(conf):
        print 'Error: no local configuration file at "'+str(conf)+'"'
        answer = None
        while answer not in 'y n yes no'.split():
          answer=raw_input('Make configuration file for directory: [y/n] ')
        if answer in 'y yes'.split():
          conf = createConf(container,conf)
          return conf
        else:
          print 'Bye!'
          sys.exit()
    else:
      D = eval(open(conf).read())
      return D

if __name__=='__main__':
    #os.system('.projectConfig')
    container = os.path.split(__file__)[0]
    conf      = getOrCreateConf(container)
    prodhost  = conf['prodhost']
    produser  = conf['produser']
    projpath  = conf['projpath']
    line      = 'ssh '+produser+'@'+prodhost+' "cd '+projpath+'; svn up"'
    print 'would have executed:\n\t',line

